package com.travelms.service.commonuserpage;

import com.travelms.pojo.Hotel;
import com.travelms.dao.DBFind;
import com.travelms.util.DataEffect;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Stack;

public class BuyHotel {
    //用stack来表示层级结构以便更好返回上一级
    DBFind dbFind = new DBFind();
    ArrayList<Hotel> HotelArrayList = null;

    public BuyHotel(String username) throws SQLException, IOException {
        Stack<Integer> stack = new Stack<>();
        Scanner sc = new Scanner(System.in);
        int current = 0;
        stack.push(current);

        while (true) {
            if (current == 0) {
                System.out.println("1.输入酒店名称查询");
                System.out.println("2.输入酒店的地址查询");
                System.out.println("3.退出");
            }
            while (true) {
                System.out.println("亲输入:");
                String s = sc.nextLine();
                if (!DataEffect.isNumber(s)) {//输入效验
                    System.out.println("输入错误");
                } else if (!(s.compareTo("0") > 0 && s.compareTo("4") < 0)) {
                    System.out.println("输入错误");
                } else {
                    current = Integer.parseInt(s);//记录输入的层级
                    stack.push(current);//压入栈
                    break;
                }
            }
            if (current == 3) {
                //为3则退出
                break;
            } else if (current == 1) {
                //输入酒店名称查询
                while (true) {
                    System.out.println("输入exit退出输入!!!");
                    System.out.println("请输入酒店名称：");

                    String dest = sc.nextLine();
                    if (dest.isEmpty()){
                        System.out.println("输入错误");
                        continue;
                    }
                    if ("exit".equals(dest)){
                        break;
                    }
                    HotelArrayList =  dbFind.findHotelName(dest);
                    if (HotelArrayList.isEmpty()){
                        System.out.println("没有该名字的酒店");
                        continue;
                    }
                    int start = HotelArrayList.get(0).getId();
                    int end = HotelArrayList.get(HotelArrayList.size()-1).getId();
                    HotelArrayList.forEach(System.out::println);
                    String opt = null;
                    while (true) {
                        System.out.println("请输入你要订购的酒店id：");
                        opt = sc.nextLine();

                        if (DataEffect.isNumber(opt) && (Integer.parseInt(opt) >= start && Integer.parseInt(opt) <= end)){
                            break;
                        }
                        System.out.println("输入错误");
                    }
                    int i = dbFind.orderHotel(opt,username);
                    if(i == 1){
                        System.out.println("余额不足，请检查余额");
                        break;
                    } else if (i == 2) {
                        System.out.println("车票数量不足,请换一张车票");
                    } else if (i == 3) {
                        System.out.println("购买失败");
                        break;
                    }else {
                        System.out.println("购买成功");
                        break;
                    }

                }
                stack.pop();
                current = stack.peek();

            } else if (current == 2) {
                //输入酒店的地址查询
                while (true) {
                    System.out.println("输入exit退出输入!!!");
                    System.out.println("请输入酒店地址：");

                    String dest = sc.nextLine();
                    if (dest.isEmpty()){
                        System.out.println("输入错误");
                        continue;
                    }
                    if ("exit".equals(dest)){
                        break;
                    }
                    HotelArrayList =  dbFind.findHotelDest(dest);
                    if (HotelArrayList.isEmpty()){
                        System.out.println("没有该名字的酒店");
                        continue;
                    }
                    int start = HotelArrayList.get(0).getId();
                    int end = HotelArrayList.get(HotelArrayList.size()-1).getId();
                    HotelArrayList.forEach(System.out::println);
                    String opt = null;
                    while (true) {
                        System.out.println("请输入你要订购的酒店id：");
                        opt = sc.nextLine();

                        if (DataEffect.isNumber(opt) && (Integer.parseInt(opt) >= start && Integer.parseInt(opt) <= end)){
                            break;
                        }
                        System.out.println("输入错误");
                    }
                    int i = dbFind.orderHotel(opt,username);
                    if(i == 1){
                        System.out.println("余额不足，请检查余额");
                        break;
                    } else if (i == 2) {
                        System.out.println("车票数量不足,请换一个酒店");
                    } else if (i == 3) {
                        System.out.println("购买失败");
                        break;
                    }else {
                        System.out.println("购买成功");
                        break;
                    }

                }
                stack.pop();
                current = stack.peek();
            }


        }

    }
}
